package br.com.andreikeda.utility.gestor.finance.storage.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import br.com.andreikeda.utility.gestor.finance.storage.database.model.Category;
import br.com.andreikeda.utility.gestor.finance.utils.ColumnConstants;
import br.com.andreikeda.utility.gestor.finance.utils.LogUtils;

/**
 * Created by LimaoGames on 22/01/2015.
 * contact me: andreikeda87@gmail.com
 */
public class CategoryDAO extends GenericDAO {

    private static CategoryDAO instance;

    private final String TABLE = "CATEGORY";

    private final String WHERE_CLAUSE_DEFAULT = "id=?";

    public static CategoryDAO getInstance() {
        if (instance == null)
            instance = new CategoryDAO();
        return instance;
    }

    @Override
    public boolean delete(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Category))
            return false;
        boolean aux = mLite.delete(TABLE, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(((Category) object).getId())}) > 0;
        if (aux) {
            ContentValues cv = new ContentValues();
            cv.put(ColumnConstants.CATEGORY_ID, 0);
            CompanyDAO.getInstance().updateAll(mLite, cv, "company_id=?", new String[]{String.valueOf(((Category) object).getId())});
        }
        return aux;
    }

    @Override
    public void deleteAll(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        mLite.delete(TABLE, whereClause, whereArgs);
    }

    @Override
    public boolean insert(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Category))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.NAME, ((Category) object).getName());
        cv.put(ColumnConstants.RESOURCE, ((Category) object).getResId());
        mLite.insert(TABLE, null, cv);
        return true;
    }

    @Override
    public List list(SQLiteDatabase mLite) {
        Cursor c = mLite.query(TABLE, null, null, null, null, null, String.format("%s ASC", ColumnConstants.NAME));
        List categories = new ArrayList<Category>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                categories.add(
                        new Category(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.NAME)),
                                c.getInt(c.getColumnIndex(ColumnConstants.RESOURCE))));
                c.moveToNext();
            }
        }
        return categories;
    }

    @Override
    public List<Object> listFrom(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        Cursor c = mLite.query(TABLE, null, whereClause, whereArgs, null, null, String.format("%s ASC", ColumnConstants.NAME));
        List categories = new ArrayList<Category>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                categories.add(
                        new Category(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.NAME)),
                                c.getInt(c.getColumnIndex(ColumnConstants.RESOURCE))));
                //c.moveToNext();
                break;
            }
        }
        return categories;
    }

    @Override
    public boolean update(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Category))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.NAME, ((Category) object).getName());
        cv.put(ColumnConstants.RESOURCE, ((Category) object).getResId());
        mLite.update(TABLE, cv, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(((Category) object).getId())});
        return true;
    }

    @Override
    public void updateAll(SQLiteDatabase mLite, ContentValues cv, String whereClause, String[] whereArgs) {
        mLite.update(TABLE, cv, whereClause, whereArgs);
    }

    private CategoryDAO() {

    }
}
